const baseUrl = "http://localhost:8081";

class Http {
  // 封装通用的fetch请求方法
  customFetch(method, path, data, headers = {}) {
    const options = {
      method: method,
      headers: {
        ...headers,
      },
      // body: data ? data : undefined,
    };
    if (data) {
      options.body = data;
    }
    console.log("options", path, options);
    return fetch(`${baseUrl}${path}`, options)
      .then((response) => {
        if (!response.ok) {
          throw new Error("Network response was not ok");
        }
        return response.json();
      })
      .catch((error) => {
        console.error("Error:", error);
      });
  }
  GET(...args) {
    return this.customFetch("GET", ...args);
  }
  POST(...args) {
    return this.customFetch("POST", ...args);
  }
}
export const http = new Http();

const login = (data) =>
  http.POST("/login", JSON.stringify(data), {
    "Content-Type": "application/json",
  });
const register = (data) =>
  http.POST("/register", JSON.stringify(data), {
    "Content-Type": "application/json",
  });
const getUserResume = (username) =>
  http.GET(`/getUserResume?username=${username}`, undefined, {
    "Content-Type": "application/json",
  });
const uploadResume = (data) => http.POST("/upload", data);

export default {
  login,
  register,
  uploadResume,
  getUserResume,
};
